3 * Base code for file repositories.
10 * Base class for file repositories
16 const DELETE_SOURCE
= 1;
18 const OVERWRITE_SAME
= 4;
19 const SKIP_VALIDATION
= 8;
21 /** @var FileBackendBase */
23 /** @var Array Map of zones to config */
24 protected $zones = array();
26 var $thumbScriptUrl, $transformVia404;
27 var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
28 var $fetchDescription, $initialCapital;
29 var $pathDisclosureProtection = 'simple'; // 'paranoid'
30 var $descriptionCacheExpiry, $url, $thumbUrl;
31 var $hashLevels, $deletedHashLevels;
34 * Factory functions for creating new files
35 * Override these in the base class
37 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
38 var $oldFileFactory = false;
39 var $fileFactoryKey = false, $oldFileFactoryKey = false;
41 function __construct( $info ) {
43 $this->name
= $info['name'];
44 $this->url
= isset( $info['url'] )
46 : false; // a subclass will need to set the URL (e.g. ForeignAPIRepo)
47 if ( $info['backend'] instanceof FileBackendBase
) {
48 $this->backend
= $info['backend']; // useful for testing
50 $this->backend
= FileBackendGroup
::singleton()->get( $info['backend'] );
53 // Optional settings that can have no value
54 $optionalSettings = array(
55 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
56 'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
59 foreach ( $optionalSettings as $var ) {
60 if ( isset( $info[$var] ) ) {
61 $this->$var = $info[$var];
65 // Optional settings that have a default
66 $this->initialCapital
= isset( $info['initialCapital'] )
67 ?
$info['initialCapital']
68 : MWNamespace
::isCapitalized( NS_FILE
);
69 $this->thumbUrl
= isset( $info['thumbUrl'] )
71 : "{$this->url}/thumb";
72 $this->hashLevels
= isset( $info['hashLevels'] )
75 $this->deletedHashLevels
= isset( $info['deletedHashLevels'] )
76 ?
$info['deletedHashLevels']
78 $this->transformVia404
= !empty( $info['transformVia404'] );
79 $this->zones
= isset( $info['zones'] )
82 // Give defaults for the basic zones...
83 foreach ( array( 'public', 'thumb', 'temp', 'deleted' ) as $zone ) {
84 if ( !isset( $this->zones
[$zone] ) ) {
85 if ( $zone === 'deleted' ) {
86 $this->zones
[$zone] = array(
87 'container' => null, // user must set this up
88 'directory' => '' // container root
91 $this->zones
[$zone] = array(
92 'container' => "media-$zone",
93 'directory' => '' // container root
101 * Get the file backend instance
103 * @return FileBackendBase
105 public function getBackend() {
106 return $this->backend
;
110 * Prepare all the zones for basic usage.
111 * See initDeletedDir() for additional setup needed for the 'deleted' zone.
113 * @param $doZones Array Only do a particular zones
116 protected function initZones( $doZones = array() ) {
117 $status = $this->newGood();
118 $doZones = (array)$doZones; // string => array
119 foreach ( $this->zones
as $zone => $info ) {
120 if ( $doZones && !in_array( $zone, $doZones ) ) {
123 $root = $this->getZonePath( $zone );
124 if ( $root !== null ) {
125 $params = array( 'dir' => $this->getZonePath( $zone ) );
126 $status->merge( $this->backend
->prepare( $params ) );
133 * Take all available measures to prevent web accessibility of new deleted
134 * directories, in case the user has not configured offline storage
138 protected function initDeletedDir( $dir ) {
139 // Add a .htaccess file to the root of the deleted zone
140 $root = $this->getZonePath( 'deleted' );
141 $this->backend
->secure( array( 'dir' => $root, 'noAccess' => true ) );
142 // Seed new directories with a blank index.html, to prevent crawling
143 $this->backend
->secure( array( 'dir' => $dir, 'noListing' => true ) );
147 * Determine if a string is an mwrepo:// URL
152 public static function isVirtualUrl( $url ) {
153 return substr( $url, 0, 9 ) == 'mwrepo://';
157 * Get a URL referring to this repository, with the private mwrepo protocol.
158 * The suffix, if supplied, is considered to be unencoded, and will be
159 * URL-encoded before being returned.
161 * @param $suffix string
164 public function getVirtualUrl( $suffix = false ) {
165 $path = 'mwrepo://' . $this->name
;
166 if ( $suffix !== false ) {
167 $path .= '/' . rawurlencode( $suffix );
173 * Get the URL corresponding to one of the four basic zones
175 * @param $zone String: one of: public, deleted, temp, thumb
176 * @return String or false
178 public function getZoneUrl( $zone ) {
183 return "{$this->url}/temp";
185 return false; // no public URL
187 return $this->thumbUrl
;
194 * Get the backend storage path corresponding to a virtual URL
199 function resolveVirtualUrl( $url ) {
200 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
201 throw new MWException( __METHOD__
.': unknown protocol' );
203 $bits = explode( '/', substr( $url, 9 ), 3 );
204 if ( count( $bits ) != 3 ) {
205 throw new MWException( __METHOD__
.": invalid mwrepo URL: $url" );
207 list( $repo, $zone, $rel ) = $bits;
208 if ( $repo !== $this->name
) {
209 throw new MWException( __METHOD__
.": fetching from a foreign repo is not supported" );
211 $base = $this->getZonePath( $zone );
213 throw new MWException( __METHOD__
.": invalid zone: $zone" );
215 return $base . '/' . rawurldecode( $rel );
219 * The the storage container and base path of a zone
221 * @param $zone string
222 * @return Array (container, base path) or (null, null)
224 protected function getZoneLocation( $zone ) {
225 if ( !isset( $this->zones
[$zone] ) ) {
226 return array( null, null ); // bogus
228 return array( $this->zones
[$zone]['container'], $this->zones
[$zone]['directory'] );
232 * Get the storage path corresponding to one of the zones
234 * @param $zone string
235 * @return string|null
237 public function getZonePath( $zone ) {
238 list( $container, $base ) = $this->getZoneLocation( $zone );
239 if ( $container === null ||
$base === null ) {
242 $backendName = $this->backend
->getName();
243 if ( $base != '' ) { // may not be set
246 return "mwstore://$backendName/{$container}{$base}";
250 * Create a new File object from the local repository
252 * @param $title Mixed: Title object or string
253 * @param $time Mixed: Time at which the image was uploaded.
254 * If this is specified, the returned object will be an
255 * instance of the repository's old file class instead of a
256 * current file. Repositories not supporting version control
257 * should return false if this parameter is set.
258 * @return File|null A File, or null if passed an invalid Title
260 public function newFile( $title, $time = false ) {
261 $title = File
::normalizeTitle( $title );
266 if ( $this->oldFileFactory
) {
267 return call_user_func( $this->oldFileFactory
, $title, $this, $time );
272 return call_user_func( $this->fileFactory
, $title, $this );
277 * Find an instance of the named file created at the specified time
278 * Returns false if the file does not exist. Repositories not supporting
279 * version control should return false if the time is specified.
281 * @param $title Mixed: Title object or string
282 * @param $options array Associative array of options:
283 * time: requested time for an archived image, or false for the
284 * current version. An image object will be returned which was
285 * created at the specified time.
287 * ignoreRedirect: If true, do not follow file redirects
289 * private: If true, return restricted (deleted) files if the current
290 * user is allowed to view them. Otherwise, such files will not
294 public function findFile( $title, $options = array() ) {
295 $title = File
::normalizeTitle( $title );
299 $time = isset( $options['time'] ) ?
$options['time'] : false;
300 # First try the current version of the file to see if it precedes the timestamp
301 $img = $this->newFile( $title );
305 if ( $img->exists() && ( !$time ||
$img->getTimestamp() == $time ) ) {
308 # Now try an old version of the file
309 if ( $time !== false ) {
310 $img = $this->newFile( $title, $time );
311 if ( $img && $img->exists() ) {
312 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
313 return $img; // always OK
314 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
321 if ( !empty( $options['ignoreRedirect'] ) ) {
324 $redir = $this->checkRedirect( $title );
325 if ( $redir && $title->getNamespace() == NS_FILE
) {
326 $img = $this->newFile( $redir );
330 if ( $img->exists() ) {
331 $img->redirectedFrom( $title->getDBkey() );
339 * Find many files at once.
341 * @param $items An array of titles, or an array of findFile() options with
342 * the "title" option giving the title. Example:
344 * $findItem = array( 'title' => $title, 'private' => true );
345 * $findBatch = array( $findItem );
346 * $repo->findFiles( $findBatch );
349 public function findFiles( $items ) {
351 foreach ( $items as $item ) {
352 if ( is_array( $item ) ) {
353 $title = $item['title'];
355 unset( $options['title'] );
360 $file = $this->findFile( $title, $options );
362 $result[$file->getTitle()->getDBkey()] = $file;
369 * Find an instance of the file with this key, created at the specified time
370 * Returns false if the file does not exist. Repositories not supporting
371 * version control should return false if the time is specified.
373 * @param $sha1 String base 36 SHA-1 hash
374 * @param $options Option array, same as findFile().
377 public function findFileFromKey( $sha1, $options = array() ) {
378 $time = isset( $options['time'] ) ?
$options['time'] : false;
380 # First try to find a matching current version of a file...
381 if ( $this->fileFactoryKey
) {
382 $img = call_user_func( $this->fileFactoryKey
, $sha1, $this, $time );
384 return false; // find-by-sha1 not supported
386 if ( $img && $img->exists() ) {
389 # Now try to find a matching old version of a file...
390 if ( $time !== false && $this->oldFileFactoryKey
) { // find-by-sha1 supported?
391 $img = call_user_func( $this->oldFileFactoryKey
, $sha1, $this, $time );
392 if ( $img && $img->exists() ) {
393 if ( !$img->isDeleted( File
::DELETED_FILE
) ) {
394 return $img; // always OK
395 } elseif ( !empty( $options['private'] ) && $img->userCan( File
::DELETED_FILE
) ) {
404 * Get an array or iterator of file objects for files that have a given
405 * SHA-1 content hash.
409 public function findBySha1( $hash ) {
414 * Get the public root URL of the repository
418 public function getRootUrl() {
423 * Returns true if the repository uses a multi-level directory structure
427 public function isHashed() {
428 return (bool)$this->hashLevels
;
432 * Get the URL of thumb.php
436 public function getThumbScriptUrl() {
437 return $this->thumbScriptUrl
;
441 * Returns true if the repository can transform files via a 404 handler
445 public function canTransformVia404() {
446 return $this->transformVia404
;
450 * Get the name of an image from its title object
452 * @param $title Title
454 public function getNameFromTitle( Title
$title ) {
456 if ( $this->initialCapital
!= MWNamespace
::isCapitalized( NS_FILE
) ) {
457 $name = $title->getUserCaseDBKey();
458 if ( $this->initialCapital
) {
459 $name = $wgContLang->ucfirst( $name );
462 $name = $title->getDBkey();
468 * Get the public zone root storage directory of the repository
472 public function getRootDirectory() {
473 return $this->getZonePath( 'public' );
477 * Get a relative path including trailing slash, e.g. f/fa/
478 * If the repo is not hashed, returns an empty string
480 * @param $name string
483 public function getHashPath( $name ) {
484 return self
::getHashPathForLevel( $name, $this->hashLevels
);
492 static function getHashPathForLevel( $name, $levels ) {
493 if ( $levels == 0 ) {
496 $hash = md5( $name );
498 for ( $i = 1; $i <= $levels; $i++
) {
499 $path .= substr( $hash, 0, $i ) . '/';
506 * Get the number of hash directory levels
510 public function getHashLevels() {
511 return $this->hashLevels
;
515 * Get the name of this repository, as specified by $info['name]' to the constructor
519 public function getName() {
524 * Make an url to this repo
526 * @param $query mixed Query string to append
527 * @param $entry string Entry point; defaults to index
530 public function makeUrl( $query = '', $entry = 'index' ) {
531 $ext = isset( $this->scriptExtension
) ?
$this->scriptExtension
: '.php';
532 return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
536 * Get the URL of an image description page. May return false if it is
537 * unknown or not applicable. In general this should only be called by the
538 * File class, since it may return invalid results for certain kinds of
539 * repositories. Use File::getDescriptionUrl() in user code.
541 * In particular, it uses the article paths as specified to the repository
542 * constructor, whereas local repositories use the local Title functions.
544 * @param $name string
547 public function getDescriptionUrl( $name ) {
548 $encName = wfUrlencode( $name );
549 if ( !is_null( $this->descBaseUrl
) ) {
550 # "http://example.com/wiki/Image:"
551 return $this->descBaseUrl
. $encName;
553 if ( !is_null( $this->articleUrl
) ) {
554 # "http://example.com/wiki/$1"
556 # We use "Image:" as the canonical namespace for
557 # compatibility across all MediaWiki versions.
558 return str_replace( '$1',
559 "Image:$encName", $this->articleUrl
);
561 if ( !is_null( $this->scriptDirUrl
) ) {
562 # "http://example.com/w"
564 # We use "Image:" as the canonical namespace for
565 # compatibility across all MediaWiki versions,
566 # and just sort of hope index.php is right. ;)
567 return $this->makeUrl( "title=Image:$encName" );
573 * Get the URL of the content-only fragment of the description page. For
574 * MediaWiki this means action=render. This should only be called by the
575 * repository's file class, since it may return invalid results. User code
576 * should use File::getDescriptionText().
578 * @param $name String: name of image to fetch
579 * @param $lang String: language to fetch it in, if any.
582 public function getDescriptionRenderUrl( $name, $lang = null ) {
583 $query = 'action=render';
584 if ( !is_null( $lang ) ) {
585 $query .= '&uselang=' . $lang;
587 if ( isset( $this->scriptDirUrl
) ) {
588 return $this->makeUrl(
590 wfUrlencode( 'Image:' . $name ) .
593 $descUrl = $this->getDescriptionUrl( $name );
595 return wfAppendQuery( $descUrl, $query );
603 * Get the URL of the stylesheet to apply to description pages
607 public function getDescriptionStylesheetUrl() {
608 if ( $this->scriptDirUrl
) {
609 return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
610 wfArrayToCGI( Skin
::getDynamicStylesheetQuery() ) );
615 * Store a file to a given destination.
617 * @param $srcPath String: source FS path, storage path, or virtual URL
618 * @param $dstZone String: destination zone
619 * @param $dstRel String: destination relative path
620 * @param $flags Integer: bitwise combination of the following flags:
621 * self::DELETE_SOURCE Delete the source file after upload
622 * self::OVERWRITE Overwrite an existing destination file instead of failing
623 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
624 * same contents as the source
625 * @return FileRepoStatus
627 public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
628 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
629 if ( $status->successCount
== 0 ) {
636 * Store a batch of files
638 * @param $triplets Array: (src, dest zone, dest rel) triplets as per store()
639 * @param $flags Integer: bitwise combination of the following flags:
640 * self::DELETE_SOURCE Delete the source file after upload
641 * self::OVERWRITE Overwrite an existing destination file instead of failing
642 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
643 * same contents as the source
644 * @return FileRepoStatus
646 public function storeBatch( $triplets, $flags = 0 ) {
647 $backend = $this->backend
; // convenience
649 // Try creating directories
650 $status = $this->initZones();
651 if ( !$status->isOK() ) {
655 $status = $this->newGood();
657 $operations = array();
658 $sourceFSFilesToDelete = array(); // cleanup for disk source files
659 // Validate each triplet and get the store operation...
660 foreach ( $triplets as $i => $triplet ) {
661 list( $srcPath, $dstZone, $dstRel ) = $triplet;
663 // Resolve destination path
664 $root = $this->getZonePath( $dstZone );
666 throw new MWException( "Invalid zone: $dstZone" );
668 if ( !$this->validateFilename( $dstRel ) ) {
669 throw new MWException( 'Validation error in $dstRel' );
671 $dstPath = "$root/$dstRel";
672 $dstDir = dirname( $dstPath );
674 // Create destination directories for this triplet
675 if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
676 return $this->newFatal( 'directorycreateerror', $dstDir );
679 if ( $dstZone == 'deleted' ) {
680 $this->initDeletedDir( $dstDir );
683 // Resolve source to a storage path if virtual
684 if ( self
::isVirtualUrl( $srcPath ) ) {
685 $srcPath = $this->resolveVirtualUrl( $srcPath );
688 // Get the appropriate file operation
689 if ( FileBackend
::isStoragePath( $srcPath ) ) {
690 $opName = ( $flags & self
::DELETE_SOURCE
) ?
'move' : 'copy';
693 if ( $flags & self
::DELETE_SOURCE
) {
694 $sourceFSFilesToDelete[] = $srcPath;
697 $operations[] = array(
701 'overwriteDest' => $flags & self
::OVERWRITE
,
702 'overwriteSame' => $flags & self
::OVERWRITE_SAME
,
706 // Execute the store operation for each triplet
707 $opts = array( 'ignoreErrors' => true );
708 $status->merge( $backend->doOperations( $operations, $opts ) );
709 // Cleanup for disk source files...
710 foreach ( $sourceFSFilesToDelete as $file ) {
711 wfSuppressWarnings();
712 unlink( $file ); // FS cleanup
720 * Deletes a batch of files.
721 * Each file can be a (zone, rel) pair, virtual url, storage path, or FS path.
722 * It will try to delete each file, but ignores any errors that may occur.
724 * @param $pairs array List of files to delete
727 public function cleanupBatch( $files ) {
728 $operations = array();
729 $sourceFSFilesToDelete = array(); // cleanup for disk source files
730 foreach ( $files as $file ) {
731 if ( is_array( $file ) ) {
732 // This is a pair, extract it
733 list( $zone, $rel ) = $file;
734 $root = $this->getZonePath( $zone );
735 $path = "$root/$rel";
737 if ( self
::isVirtualUrl( $file ) ) {
738 // This is a virtual url, resolve it
739 $path = $this->resolveVirtualUrl( $file );
741 // This is a full file name
745 // Get a file operation if needed
746 if ( FileBackend
::isStoragePath( $path ) ) {
747 $operations[] = array(
752 $sourceFSFilesToDelete[] = $path;
755 // Actually delete files from storage...
756 $opts = array( 'ignoreErrors' => true );
757 $this->backend
->doOperations( $operations, $opts );
758 // Cleanup for disk source files...
759 foreach ( $sourceFSFilesToDelete as $file ) {
760 wfSuppressWarnings();
761 unlink( $path ); // FS cleanup
767 * Pick a random name in the temp zone and store a file to it.
768 * Returns a FileRepoStatus object with the URL in the value.
770 * @param $originalName String: the base name of the file as specified
771 * by the user. The file extension will be maintained.
772 * @param $srcPath String: the current location of the file.
773 * @return FileRepoStatus object with the URL in the value.
775 public function storeTemp( $originalName, $srcPath ) {
776 $date = gmdate( "YmdHis" );
777 $hashPath = $this->getHashPath( $originalName );
778 $dstRel = "{$hashPath}{$date}!{$originalName}";
779 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
781 $result = $this->store( $srcPath, 'temp', $dstRel );
782 $result->value
= $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
787 * Concatenate a list of files into a target file location.
789 * @param $srcPaths Array Ordered list of source virtual URLs/storage paths
790 * @param $dstPath String Target virtual URL/storage path
791 * @param $flags Integer: bitwise combination of the following flags:
792 * self::DELETE_SOURCE Delete the source files
793 * @return FileRepoStatus
795 function concatenate( $srcPaths, $dstPath, $flags = 0 ) {
796 $status = $this->newGood();
797 // Resolve target to a storage path if virtual
798 $dest = $this->resolveToStoragePath( $dstPath );
801 $deleteOperations = array(); // post-concatenate ops
802 foreach ( $srcPaths as $srcPath ) {
803 // Resolve source to a storage path if virtual
804 $source = $this->resolveToStoragePath( $srcPath );
805 $sources[] = $source; // chunk to merge
806 if ( $flags & self
::DELETE_SOURCE
) {
807 $deleteOperations[] = array( 'op' => 'delete', 'src' => $source );
811 // Concatenate the chunks into one file
812 $op = array( 'op' => 'concatenate',
813 'srcs' => $sources, 'dst' => $dest, 'overwriteDest' => true );
814 $status->merge( $this->backend
->doOperation( $op ) );
815 if ( !$status->isOK() ) {
819 // Delete the sources if required
820 if ( $deleteOperations ) {
821 $opts = array( 'ignoreErrors' => true );
822 $status->merge( $this->backend
->doOperations( $deleteOperations, $opts ) );
825 // Make sure status is OK, despite any $deleteOperations fatals
826 $status->setResult( true );
832 * Remove a temporary file or mark it for garbage collection
834 * @param $virtualUrl String: the virtual URL returned by storeTemp
835 * @return Boolean: true on success, false on failure
837 public function freeTemp( $virtualUrl ) {
838 $temp = "mwrepo://{$this->name}/temp";
839 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
840 wfDebug( __METHOD__
.": Invalid temp virtual URL\n" );
843 $path = $this->resolveVirtualUrl( $virtualUrl );
844 $op = array( 'op' => 'delete', 'src' => $path );
845 $status = $this->backend
->doOperation( $op );
846 return $status->isOK();
850 * Copy or move a file either from a storage path, virtual URL,
851 * or FS path, into this repository at the specified destination location.
853 * Returns a FileRepoStatus object. On success, the value contains "new" or
854 * "archived", to indicate whether the file was new with that name.
856 * @param $srcPath String: the source FS path, storage path, or URL
857 * @param $dstRel String: the destination relative path
858 * @param $archiveRel String: the relative path where the existing file is to
859 * be archived, if there is one. Relative to the public zone root.
860 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
861 * that the source file should be deleted if possible
863 public function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
864 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
865 if ( $status->successCount
== 0 ) {
868 if ( isset( $status->value
[0] ) ) {
869 $status->value
= $status->value
[0];
871 $status->value
= false;
877 * Publish a batch of files
879 * @param $triplets Array: (source, dest, archive) triplets as per publish()
880 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
881 * that the source files should be deleted if possible
882 * @return FileRepoStatus
884 public function publishBatch( $triplets, $flags = 0 ) {
885 $backend = $this->backend
; // convenience
887 // Try creating directories
888 $status = $this->initZones( 'public' );
889 if ( !$status->isOK() ) {
893 $status = $this->newGood( array() );
895 $operations = array();
896 $sourceFSFilesToDelete = array(); // cleanup for disk source files
897 // Validate each triplet and get the store operation...
898 foreach ( $triplets as $i => $triplet ) {
899 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
900 // Resolve source to a storage path if virtual
901 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
902 $srcPath = $this->resolveVirtualUrl( $srcPath );
904 if ( !$this->validateFilename( $dstRel ) ) {
905 throw new MWException( 'Validation error in $dstRel' );
907 if ( !$this->validateFilename( $archiveRel ) ) {
908 throw new MWException( 'Validation error in $archiveRel' );
911 $publicRoot = $this->getZonePath( 'public' );
912 $dstPath = "$publicRoot/$dstRel";
913 $archivePath = "$publicRoot/$archiveRel";
915 $dstDir = dirname( $dstPath );
916 $archiveDir = dirname( $archivePath );
917 // Abort immediately on directory creation errors since they're likely to be repetitive
918 if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
919 return $this->newFatal( 'directorycreateerror', $dstDir );
921 if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
922 return $this->newFatal( 'directorycreateerror', $archiveDir );
925 // Archive destination file if it exists
926 if ( $backend->fileExists( array( 'src' => $dstPath ) ) ) {
927 // Check if the archive file exists
928 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
929 // unlinks the destination file if it exists. DB-based synchronisation in
930 // publishBatch's caller should prevent races. In Windows there's no
931 // problem because the rename primitive fails if the destination exists.
932 if ( $backend->fileExists( array( 'src' => $archivePath ) ) ) {
933 $operations[] = array( 'op' => 'null' );
936 $operations[] = array(
939 'dst' => $archivePath
942 $status->value
[$i] = 'archived';
944 $status->value
[$i] = 'new';
946 // Copy (or move) the source file to the destination
947 if ( FileBackend
::isStoragePath( $srcPath ) ) {
948 if ( $flags & self
::DELETE_SOURCE
) {
949 $operations[] = array(
955 $operations[] = array(
961 } else { // FS source path
962 $operations[] = array(
967 if ( $flags & self
::DELETE_SOURCE
) {
968 $sourceFSFilesToDelete[] = $srcPath;
973 // Execute the operations for each triplet
974 $opts = array( 'ignoreErrors' => true );
975 $status->merge( $backend->doOperations( $operations, $opts ) );
976 // Cleanup for disk source files...
977 foreach ( $sourceFSFilesToDelete as $file ) {
978 wfSuppressWarnings();
979 unlink( $file ); // FS cleanup
987 * Checks existence of a a file
989 * @param $file Virtual URL (or storage path) of file to check
990 * @param $flags Integer: bitwise combination of the following flags:
991 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
994 public function fileExists( $file, $flags = 0 ) {
995 $result = $this->fileExistsBatch( array( $file ), $flags );
1000 * Checks existence of an array of files.
1002 * @param $files Array: Virtual URLs (or storage paths) of files to check
1003 * @param $flags Integer: bitwise combination of the following flags:
1004 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
1005 * @return Either array of files and existence flags, or false
1007 public function fileExistsBatch( $files, $flags = 0 ) {
1008 if ( !$this->initZones() ) {
1012 foreach ( $files as $key => $file ) {
1013 if ( self
::isVirtualUrl( $file ) ) {
1014 $file = $this->resolveVirtualUrl( $file );
1016 if ( FileBackend
::isStoragePath( $file ) ) {
1017 $result[$key] = $this->backend
->fileExists( array( 'src' => $file ) );
1019 if ( $flags & self
::FILES_ONLY
) {
1020 $result[$key] = is_file( $file ); // FS only
1022 $result[$key] = file_exists( $file ); // FS only
1031 * Move a file to the deletion archive.
1032 * If no valid deletion archive exists, this may either delete the file
1033 * or throw an exception, depending on the preference of the repository
1035 * @param $srcRel Mixed: relative path for the file to be deleted
1036 * @param $archiveRel Mixed: relative path for the archive location.
1037 * Relative to a private archive directory.
1038 * @return FileRepoStatus object
1040 public function delete( $srcRel, $archiveRel ) {
1041 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
1045 * Move a group of files to the deletion archive.
1047 * If no valid deletion archive is configured, this may either delete the
1048 * file or throw an exception, depending on the preference of the repository.
1050 * The overwrite policy is determined by the repository -- currently LocalRepo
1051 * assumes a naming scheme in the deleted zone based on content hash, as
1052 * opposed to the public zone which is assumed to be unique.
1054 * @param $sourceDestPairs Array of source/destination pairs. Each element
1055 * is a two-element array containing the source file path relative to the
1056 * public root in the first element, and the archive file path relative
1057 * to the deleted zone root in the second element.
1058 * @return FileRepoStatus
1060 public function deleteBatch( $sourceDestPairs ) {
1061 $backend = $this->backend
; // convenience
1063 if ( !isset( $this->zones
['deleted']['container'] ) ) {
1064 throw new MWException( __METHOD__
.': no valid deletion archive directory' );
1067 // Try creating directories
1068 $status = $this->initZones( array( 'public', 'deleted' ) );
1069 if ( !$status->isOK() ) {
1073 $status = $this->newGood();
1075 $operations = array();
1076 // Validate filenames and create archive directories
1077 foreach ( $sourceDestPairs as $pair ) {
1078 list( $srcRel, $archiveRel ) = $pair;
1079 if ( !$this->validateFilename( $srcRel ) ) {
1080 throw new MWException( __METHOD__
.':Validation error in $srcRel' );
1082 if ( !$this->validateFilename( $archiveRel ) ) {
1083 throw new MWException( __METHOD__
.':Validation error in $archiveRel' );
1086 $publicRoot = $this->getZonePath( 'public' );
1087 $srcPath = "{$publicRoot}/$srcRel";
1089 $deletedRoot = $this->getZonePath( 'deleted' );
1090 $archivePath = "{$deletedRoot}/{$archiveRel}";
1091 $archiveDir = dirname( $archivePath ); // does not touch FS
1093 // Create destination directories
1094 if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
1095 return $this->newFatal( 'directorycreateerror', $archiveDir );
1097 $this->initDeletedDir( $archiveDir );
1099 if ( $backend->fileExists( array( 'src' => $archivePath ) ) ) {
1100 $operations[] = array(
1105 $operations[] = array(
1108 'dst' => $archivePath
1113 // Move the files by execute the operations for each pair.
1114 // We're now committed to returning an OK result, which will
1115 // lead to the files being moved in the DB also.
1116 $opts = array( 'ignoreErrors' => true );
1117 $status->merge( $backend->doOperations( $operations, $opts ) );
1123 * Get a relative path for a deletion archive key,
1124 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
1128 public function getDeletedHashPath( $key ) {
1130 for ( $i = 0; $i < $this->deletedHashLevels
; $i++
) {
1131 $path .= $key[$i] . '/';
1137 * If a path is a virtual URL, resolve it to a storage path.
1138 * Otherwise, just return the path as it is.
1140 * @param $path string
1142 * @throws MWException
1144 protected function resolveToStoragePath( $path ) {
1145 if ( $this->isVirtualUrl( $path ) ) {
1146 return $this->resolveVirtualUrl( $path );
1152 * Get a local FS copy of a file with a given virtual URL/storage path.
1153 * Temporary files may be purged when the file object falls out of scope.
1155 * @param $virtualUrl string
1156 * @return TempFSFile|null Returns null on failure
1158 public function getLocalCopy( $virtualUrl ) {
1159 $path = $this->resolveToStoragePath( $virtualUrl );
1160 return $this->backend
->getLocalCopy( array( 'src' => $path ) );
1164 * Get a local FS file with a given virtual URL/storage path.
1165 * The file is either an original or a copy. It should not be changed.
1166 * Temporary files may be purged when the file object falls out of scope.
1168 * @param $virtualUrl string
1169 * @return FSFile|null Returns null on failure.
1171 public function getLocalReference( $virtualUrl ) {
1172 $path = $this->resolveToStoragePath( $virtualUrl );
1173 return $this->backend
->getLocalReference( array( 'src' => $path ) );
1177 * Get properties of a file with a given virtual URL/storage path.
1178 * Properties should ultimately be obtained via FSFile::getProps().
1180 * @param $virtualUrl string
1183 public function getFileProps( $virtualUrl ) {
1184 $path = $this->resolveToStoragePath( $virtualUrl );
1185 return $this->backend
->getFileProps( array( 'src' => $path ) );
1189 * Get the timestamp of a file with a given virtual URL/storage path
1191 * @param $virtualUrl string
1192 * @return string|false
1194 public function getFileTimestamp( $virtualUrl ) {
1195 $path = $this->resolveToStoragePath( $virtualUrl );
1196 return $this->backend
->getFileTimestamp( array( 'src' => $path ) );
1200 * Get the sha1 of a file with a given virtual URL/storage path
1202 * @param $virtualUrl string
1203 * @return string|false
1205 public function getFileSha1( $virtualUrl ) {
1206 $path = $this->resolveToStoragePath( $virtualUrl );
1207 $tmpFile = $this->backend
->getLocalReference( array( 'src' => $path ) );
1211 return $tmpFile->getSha1Base36();
1215 * Attempt to stream a file with the given virtual URL/storage path
1217 * @param $virtualUrl string
1218 * @param $headers Array Additional HTTP headers to send on success
1219 * @return bool Success
1221 public function streamFile( $virtualUrl, $headers = array() ) {
1222 $path = $this->resolveToStoragePath( $virtualUrl );
1223 $params = array( 'src' => $path, 'headers' => $headers );
1224 return $this->backend
->streamFile( $params )->isOK();
1228 * Call a callback function for every public file in the repository.
1229 * May use either the database or the filesystem.
1231 * @param $callback Array|string
1234 public function enumFiles( $callback ) {
1235 return $this->enumFilesInStorage( $callback );
1239 * Call a callback function for every public file in the repository.
1240 * May use either the database or the filesystem.
1242 * @param $callback Array|string
1245 protected function enumFilesInStorage( $callback ) {
1246 $publicRoot = $this->getZonePath( 'public' );
1247 $numDirs = 1 << ( $this->hashLevels
* 4 );
1248 // Use a priori assumptions about directory structure
1249 // to reduce the tree height of the scanning process.
1250 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++
) {
1251 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
1252 $path = $publicRoot;
1253 for ( $hexPos = 0; $hexPos < $this->hashLevels
; $hexPos++
) {
1254 $path .= '/' . substr( $hexString, 0, $hexPos +
1 );
1256 $iterator = $this->backend
->getFileList( array( 'dir' => $path ) );
1257 foreach ( $iterator as $name ) {
1258 // Each item returned is a public file
1259 call_user_func( $callback, "{$path}/{$name}" );
1265 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
1267 * @param $filename string
1270 public function validateFilename( $filename ) {
1271 if ( strval( $filename ) == '' ) {
1274 if ( wfIsWindows() ) {
1275 $filename = strtr( $filename, '\\', '/' );
1278 * Use the same traversal protection as Title::secureAndSplit()
1280 if ( strpos( $filename, '.' ) !== false &&
1281 ( $filename === '.' ||
$filename === '..' ||
1282 strpos( $filename, './' ) === 0 ||
1283 strpos( $filename, '../' ) === 0 ||
1284 strpos( $filename, '/./' ) !== false ||
1285 strpos( $filename, '/../' ) !== false ) )
1294 * Get a callback function to use for cleaning error message parameters
1298 function getErrorCleanupFunction() {
1299 switch ( $this->pathDisclosureProtection
) {
1301 $callback = array( $this, 'passThrough' );
1304 $callback = array( $this, 'simpleClean' );
1306 default: // 'paranoid'
1307 $callback = array( $this, 'paranoidClean' );
1313 * Path disclosure protection function
1315 * @param $param string
1318 function paranoidClean( $param ) {
1323 * Path disclosure protection function
1325 * @param $param string
1328 function simpleClean( $param ) {
1330 if ( !isset( $this->simpleCleanPairs
) ) {
1331 $this->simpleCleanPairs
= array(
1332 $IP => '$IP', // sanity
1335 return strtr( $param, $this->simpleCleanPairs
);
1339 * Path disclosure protection function
1341 * @param $param string
1344 function passThrough( $param ) {
1349 * Create a new fatal error
1351 * @return FileRepoStatus
1353 function newFatal( $message /*, parameters...*/ ) {
1354 $params = func_get_args();
1355 array_unshift( $params, $this );
1356 return MWInit
::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
1360 * Create a new good result
1362 * @return FileRepoStatus
1364 function newGood( $value = null ) {
1365 return FileRepoStatus
::newGood( $this, $value );
1369 * Delete files in the deleted directory if they are not referenced in the filearchive table
1373 public function cleanupDeletedBatch( $storageKeys ) {}
1376 * Checks if there is a redirect named as $title. If there is, return the
1377 * title object. If not, return false.
1380 * @param $title Title of image
1383 public function checkRedirect( Title
$title ) {
1388 * Invalidates image redirect cache related to that image
1389 * Doesn't do anything for repositories that don't support image redirects.
1392 * @param $title Title of image
1394 public function invalidateImageRedirect( Title
$title ) {}
1397 * Get the human-readable name of the repo
1401 public function getDisplayName() {
1402 // We don't name our own repo, return nothing
1403 if ( $this->isLocal() ) {
1406 // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
1407 return wfMessageFallback( 'shared-repo-name-' . $this->name
, 'shared-repo' )->text();
1411 * Returns true if this the local file repository.
1415 public function isLocal() {
1416 return $this->getName() == 'local';
1420 * Get a key on the primary cache for this repository.
1421 * Returns false if the repository's cache is not accessible at this site.
1422 * The parameters are the parts of the key, as for wfMemcKey().
1426 function getSharedCacheKey( /*...*/ ) {
1431 * Get a key for this repo in the local cache domain. These cache keys are
1432 * not shared with remote instances of the repo.
1433 * The parameters are the parts of the key, as for wfMemcKey().
1437 function getLocalCacheKey( /*...*/ ) {
1438 $args = func_get_args();
1439 array_unshift( $args, 'filerepo', $this->getName() );
1440 return call_user_func_array( 'wfMemcKey', $args );
1444 * Get an UploadStash associated with this repo.
1446 * @return UploadStash
1448 public function getUploadStash() {
1449 return new UploadStash( $this );